Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
now-and-later
Advanced tools
Map over an array or object of values in parallel or series, passing each through the async iterator, with optional lifecycle hooks.
The now-and-later npm package is designed to execute functions in a specific order, allowing for both series and parallel execution with support for mapping and iterating over collections. It provides a way to manage and coordinate asynchronous tasks, making it easier to handle complex flows in Node.js applications.
Map Series
Executes an iterator function on each item in an array, in series. Each iterator is called with the item, key, and a callback function. The results are collected and passed to the final callback.
const nal = require('now-and-later');
nal.mapSeries(['a', 'b', 'c'], function(value, key, cb) {
setTimeout(function() {
cb(null, value.toUpperCase());
}, 1000);
}, function(err, results) {
console.log(results); // ['A', 'B', 'C']
});
Map
Similar to mapSeries, but executes the iterator functions in parallel. The final callback is called once all iterators have completed.
const nal = require('now-and-later');
nal.map(['a', 'b', 'c'], function(value, key, cb) {
setTimeout(function() {
cb(null, value.toUpperCase());
}, 1000);
}, function(err, results) {
console.log(results); // ['A', 'B', 'C']
});
Map Values
Executes an iterator function on each property of an object, in parallel. The iterator is called with the value, key, and a callback function. The results are collected into an object and passed to the final callback.
const nal = require('now-and-later');
const obj = { a: 1, b: 2, c: 3 };
nal.mapValues(obj, function(value, key, cb) {
setTimeout(function() {
cb(null, value * 2);
}, 1000);
}, function(err, results) {
console.log(results); // { a: 2, b: 4, c: 6 }
});
Map Values Series
Works like mapValues but executes the iterator functions in series.
const nal = require('now-and-later');
const obj = { a: 1, b: 2, c: 3 };
nal.mapValuesSeries(obj, function(value, key, cb) {
setTimeout(function() {
cb(null, value * 2);
}, 1000);
}, function(err, results) {
console.log(results); // { a: 2, b: 4, c: 6 }
});
The async package is a comprehensive collection of asynchronous control-flow utilities. It provides more than 70 functions including various forms of parallel, series, and race, as well as other utility functions. It is more widely used and has a larger community compared to now-and-later.
Bluebird is a fully-featured promise library with a focus on innovative features and performance. It allows for conversion of callback-based functions to promises, which can then be used to manage asynchronous control flow. Bluebird offers more features related to promises compared to now-and-later.
Q is a promise library for JavaScript that provides a robust way of working with asynchronous operations. It allows you to create and manage promises, which can be used to organize asynchronous code more effectively. Q is more focused on promises than now-and-later, which uses callbacks.
Map over an array or object of values in parallel or series, passing each through the async iterator, with optional lifecycle hooks.
var nal = require('now-and-later');
function iterator(value, key, cb) {
// called with each value in sequence
// also passes the key
cb(null, value * 2);
}
function create(value, key) {
// called at the beginning of every iteration
// return a storage object to be passed to each lifecycle method
return { key: key, value: value };
}
function before(storage) {
// called before the iterator function of every iteration
// receives the storage returned from `create`
}
function after(result, storage) {
// called after a success occurs in the iterator function of any iteration
// receives the `result` of the iterator and the storage returned from `create`
}
function error(error, storage) {
// called after an error occurs in the iterator function of any iteration
// receives the `error` of the iterator and the storage returned from `create`
}
function done(error, results) {
// called after all iterations complete or an error occurs in an iterator
// receives an `error` if one occurred and all results (or partial results upon error) of the iterators
}
/*
Calling mapSeries with an object can't guarantee order
It uses Object.keys to get an order
It is better to use an array if order must be guaranteed
*/
nal.mapSeries(
[1, 2, 3],
iterator,
{
create: create,
before: before,
after: after,
error: error,
},
done
);
nal.map(
{
iter1: 1,
iter2: 2,
},
iterator,
{
create: create,
before: before,
after: after,
error: error,
},
done
);
map(values, iterator[, options][, callback])
Takes an object or array of values
and an iterator
function to execute with each value.
Optionally, takes an options
object and a callback
function that is called upon completion of the iterations.
All iterations run in parallel.
values
An array or object of values to iterate over.
If values
is an array, iterations are started in order by index. If values
is an object, iterations are started in order by the order returned by Object.keys
(order is not guaranteed).
If values
is an array, the results of each iteration will be mapped to an array. If values
is an object, the results of each iteration will be mapped to an object with corresponding keys.
iterator(value, key, done)
An async function called per iteration. All iterations are run in parallel.
The iterator
function is called once with each value
, key
and a function (done(error, result)
) to call when the async work is complete.
If done
is passed an error as the first argument, the iteration will fail and the sequence will be ended; however, any iterations in progress will still complete. If done
is passed a result
value as the second argument, it will be added to the final results array or object.
options
The options
object is primarily used for specifying functions that give insight into the lifecycle of each iteration. The possible extension points are create
, before
, after
and error
. If an extension point is not specified, it defaults to a no-op function.
The options
object for map
also allows specifying concurrency
in which to run your iterations. By default, your iterations will run at maximum concurrency.
options.concurrency
Limits the amount of iterations allowed to run at a given time.
options.create(value, key)
Called at the very beginning of each iteration with the value
being iterated and the key
from the array or object. If create
returns a value (storage
), it is passed to the before
, after
and error
extension points.
If a value is not returned, an empty object is used as storage
for each other extension point.
This is useful for tracking information across an iteration.
options.before(storage)
Called immediately before each iteration with the storage
value returned from the create
extension point.
options.after(result, storage)
Called immediately after each iteration with the result
of the iteration and the storage
value returned from the create
extension point.
options.error(error, storage)
Called immediately after a failed iteration with the error
of the iteration and the storage
value returned from the create
extension point.
callback(error, results)
A function that is called after all iterations have completed or one iteration has errored.
If all iterations completed successfully, the error
argument will be empty and the results
will be a mapping of the iterator
results.
If an iteration errored, the error
argument will be passed from that iteration and the results
will be whatever partial results had completed successfully before the error occurred.
mapSeries(values, iterator[, options][, callback])
Takes an object or array of values
and an iterator
function to execute with each value.
Optionally, takes an options
object and a callback
function that is called upon completion of the iterations.
All iterations run in serial.
values
An array or object of values to iterate over.
If values
is an array, iterations are started in order by index. If values
is an object, iterations are started in order by the order returned by Object.keys
(order is not guaranteed).
If values
is an array, the results of each iteration will be mapped to an array. If values
is an object, the results of each iteration will be mapped to an object with corresponding keys.
iterator(value, key, done)
An async function called per iteration. All iterations are run in serial.
The iterator
function is called once with each value
, key
and a function (done(error, result)
) to call when the async work is complete.
If done
is passed an error as the first argument, the iteration will fail and the sequence will be ended without executing any more iterations. If done
is passed a result
value as the second argument, it will be added to the final results array or object.
options
The options
object is primarily used for specifying functions that give insight into the lifecycle of each iteration. The possible extension points are create
, before
, after
and error
. If an extension point is not specified, it defaults to a no-op function.
options.create(value, key)
Called at the very beginning of each iteration with the value
being iterated and the key
from the array or object. If create
returns a value (storage
), it is passed to the before
, after
and error
extension points.
If a value is not returned, an empty object is used as storage
for each other extension point.
This is useful for tracking information across an iteration.
options.before(storage)
Called immediately before each iteration with the storage
value returned from the create
extension point.
options.after(result, storage)
Called immediately after each iteration with the result
of the iteration and the storage
value returned from the create
extension point.
options.error(error, storage)
Called immediately after a failed iteration with the error
of the iteration and the storage
value returned from the create
extension point.
callback(error, results)
A function that is called after all iterations have completed or one iteration has errored.
If all iterations completed successfully, the error
argument will be empty and the results
will be a mapping of the iterator
results.
If an iteration errored, the error
argument will be passed from that iteration and the results
will be whatever partial results had completed successfully before the error occurred.
MIT
3.0.0 (2022-06-25)
FAQs
Map over an array or object of values in parallel or series, passing each through the async iterator, with optional lifecycle hooks.
The npm package now-and-later receives a total of 772,234 weekly downloads. As such, now-and-later popularity was classified as popular.
We found that now-and-later demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.